﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Adapters.Adapters;
using WebApi.Adapters.Interfaces;
using WebApi.Model;
using WebApi.Models;

namespace WebApi.Controllers
{
    public class apiMoviesController : ApiController
    {
        private IMovieAdapter _adapter;

        public apiMoviesController()
        {
            _adapter = new MovieAdapter();
        }

        public apiMoviesController(IMovieAdapter a)
        {
            _adapter = a;
        }

        public IHttpActionResult Get()
        {
            List<MovieVM> movies = _adapter.GetAllMovies();
            return Ok(movies);
        }

        public IHttpActionResult Post(Movie model)
        {
            if (ModelState.IsValid)
            {
                int id = _adapter.AddMovie(model);
                return Ok(id);
            }
            return BadRequest("Could not add the movie to the database");
        }

        public IHttpActionResult Delete(int id)
        {
            int result = _adapter.DeleteMovie(id);
            if (result == 1) return Ok();
            else return BadRequest();
        }
    }
}